package com.jayqqaa12.robot.ctrls;

import java.util.HashMap;
import java.util.List;

import weiboclient4j.model.Status;

import com.jayqqaa12.jbase.jfinal.ext.ctrl.JsonController;
import com.jayqqaa12.robot.deprecated.SinaLogin;
import com.jayqqaa12.robot.deprecated.WeiboUser;
import com.jayqqaa12.robot.lottery.LotteryService;
import com.jayqqaa12.robot.lottery.SinaService;
import com.jayqqaa12.robot.lottery.TokenService;
import com.jayqqaa12.robot.model.User;
import com.jfinal.ext.route.ControllerBind;

@ControllerBind(controllerKey = "/")
public class IndexCtrl extends JsonController {

	public void login() {
		
		WeiboUser u = SinaLogin.login(getPara("username"), getPara("pwd"),
				(HashMap<String, String>) getSessionAttr("param"), getPara("door"));
		
		
		if (u != null) {

			if (u.getParam()!=null) {
				setSessionAttr("param", u.getParam());

				setJsonData(u.getParam().get("checkImg") );
				json.code=444;
				sendJson( );
			} else {
			
				String token = new TokenService().getToken(u.cookies);
				User.dao.save(u.getUid());
				setCookie("uid", u.getUid() + "", 60 * 60 * 24 * 365);
				setCookie("token", token, 60 * 60 * 12);

				sendJson(200);
			}
		} else sendJson(500);

	}

	public void list() {

		List<Status> list = SinaService.statusList(getCookie("token"));

		setJsonData("lotteryList", SinaService.filterLottyeStatus(list, getCookie("uid")));

		setJsonData("list", list);

		sendJson();

	}

	public void lottery() {

		sendJson(
				"list",
				new LotteryService().lottery(getPara("id"), getParaToBoolean("followMe", false), null,
						getParaToInt("count", 1), getCookie("token"), getCookie("uid")));

	}

}
